import axios from 'axios'
// import { Toast } from 'vant'
// import store from '@/store'
// import { getToken } from '@/utils/auth'

// 创建实例
const service = axios.create({
  baseURL: 'http://amall-api.unmcc.com',
  timeout: 15000 * 999,
  validateStatus: status => {
    return status < 600
  },
  headers: {
    'Content-Type': 'application/json;charset=utf-8'
    // 'Content-Type': 'application/ x-www-form-urlencoded;charset=utf-8'
  }
})

// 请求拦截器
service.interceptors.request.use(
  config => {
    config.data = JSON.stringify(config.data)
    if (config.data) {
      console.log(config.data)
    }
    const defaultToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIiLCJhdWQiOiIiLCJpYXQiOjE1Nzg2MTk5NzAsIm5iZiI6MTU3ODYxOTk3MCwiZXhwIjoxODkzOTc5OTcwLCJ1aWQiOjJ9.i4t6zoFB4xedx-xtFOMv2GBivkDCDnoKCPDMEelp7Q0'
    if (!localStorage.getItem('token')) {
      // Toast('未登录')
    }
    const token = localStorage.getItem('token') || defaultToken
    if (token) {
      config.headers['authorization'] = `Bearer ${token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  response => {
    const { data } = response
    const { method, url } = response.config
    const style = 'font-weight:bold;width:50px; border-bottom:1px solid; border-radius:3px; padding: 0 10px 0 0; background:#FFE454; color:#000;'
    console.groupCollapsed('%cresponse-log:', `${style}padding-left:5px`, `[ ${method} ]`, url)
    // console.log('%c response-log :', style, `[ ${method} ]`, url)
    console.log('%c response-data:', style, data)
    console.groupEnd()

    return data
  },
  error => {
    return Promise.reject(error)
  }
)

export default service
